Blockly.Python['pinio_package'] = function(block) {
    //return "import RPi.GPIO as GPIO \nGPIO.setmode(GPIO.BOARD) \nGPIO.setwarnings(False) \n"
    return "import RPi.GPIO as GPIO \n";
};

Blockly.Python['pinio_mode'] = function(block) {
    var gpio_mode = parseInt(block.getFieldValue('GPIO_MODE'));
    // return [gpio_mode, Blockly.Python.ORDER_ATOMIC];
    return [gpio_mode==0?'GPIO.BOARD':'GPIO.BCM', Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_get_mode'] = function(block) {
    return ["GPIO.getmode()", Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_set_mode'] = function(block) {
    var gpio_mode = block.getFieldValue('GPIO_MODE') || 0;
    if(gpio_mode==0){
        gpio_mode = 'GPIO.BOARD';
    }else {
        gpio_mode = 'GPIO.BCM';
    }

    return "GPIO.setmode(" + gpio_mode + "); \n";
};

// Blockly.Python['pinio_pin_number'] = function(block) {
//     var pin_number = parseInt(block.getFieldValue('PIN_NUMBER'));
//     return [pin_number, Blockly.Python.ORDER_ATOMIC];
// };

Blockly.Python['pinio_pin_mode'] = function(block) {
    var pin_mode = parseInt(block.getFieldValue('PIN_MODE'));
    return [pin_mode, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_set_pin_mode'] = function(block) {
    var pin_number = Blockly.Python.valueToCode(block, 'PIN_NUMBER', Blockly.Python.ORDER_ATOMIC);
    var pin_mode = parseInt(block.getFieldValue('PIN_MODE'));
    if (pin_mode == 0) {
        var resistor = parseInt(block.getFieldValue('PIN_RESISTOR'));
        return 'GPIO.setup(' + pin_number + ', GPIO.IN, pull_up_down=' + (resistor == 0 ? 'GPIO.PUD_UP' : 'GPIO.PUD_DOWN') + ') \n';
    } else {
        var level = parseInt(block.getFieldValue('INIT_LEVEL'));
        return 'GPIO.setup(' + pin_number + ', GPIO.OUT, initial=' + (level == 0 ? 'GPIO.LOW' : 'GPIO.HIGH') + ') \n';
    }

    return
};

Blockly.Python['pinio_pin_level'] = function(block) {
    var pin_level = parseInt(block.getFieldValue('PIN_LEVEL'));
    // return [pin_level, Blockly.Python.ORDER_ATOMIC];
    return [pin_level==0?'GPIO.LOW':'GPIO.HIGH', Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_set_pin_level'] = function(block) {
    var pin_number = Blockly.Python.valueToCode(block, 'PIN_NUMBER', Blockly.Python.ORDER_ATOMIC);
    var pin_level = parseInt(block.getFieldValue('PIN_LEVEL'));

    return 'GPIO.output(' + pin_number + ', ' + (pin_level==0?'GPIO.LOW':'GPIO.HIGH') + ') \n';
};

Blockly.Python['pinio_get_pin_level'] = function(block) {
    var pin_number = Blockly.Python.valueToCode(block, 'PIN_NUMBER', Blockly.Python.ORDER_ATOMIC);
    var code = 'GPIO.input(' + pin_number + ')';

    return [code, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_pin_event'] = function(block) {
    var pin_event = block.getFieldValue('PIN_EVENT');
    return [pin_event, Blockly.Python.ORDER_ATOMIC];
};

Blockly.Python['pinio_add_event_detect'] = function(block) {
    var pin_number = Blockly.Python.valueToCode(block, 'PIN_NUMBER', Blockly.Python.ORDER_ATOMIC);
    var pin_event = Blockly.Python.valueToCode(block, 'PIN_EVENT', Blockly.Python.ORDER_ATOMIC);
    var cb = Blockly.Python.valueToCode(block, 'CALLBACK', Blockly.Python.ORDER_ATOMIC);
    var bounce = Blockly.Python.valueToCode(block, 'BOUNCE', Blockly.Python.ORDER_ATOMIC);

    return 'GPIO.add_event_detect(' + pin_number + ', ' + pin_event + ', callback=' + cb + ', bouncetime=' + bounce + ') \n';
};

Blockly.Python['pinio_remove_event_detect'] = function(block) {
    var pin_number = Blockly.Python.valueToCode(block, 'PIN_NUMBER', Blockly.Python.ORDER_ATOMIC);
    return 'GPIO.remove_event_detect(' + pin_number + ') \n';
};

Blockly.Python['pinio_cleanup'] = function(block) {
    return 'GPIO.cleanup() \n';
};

